﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>生产工单选择页面</title>
    <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <form class="layui-form layui-row">
        <div class="layui-col-md4">
            <div class="layui-input-wrap">
                <label class="layui-form-label">工单名称：</label>
                <input type="text" style="width:170px" id="WorkorderName" value="" placeholder="请输入工单名称" class="layui-input" lay-affix="clear">
            </div>
        </div>
        <div class="layui-col-md4">
            <div class="layui-input-wrap">
                <label class="layui-form-label">工单编号：</label>
                <input type="text" style="width:170px" id="WorkorderCode" value="" placeholder="请输入工单编号" class="layui-input" lay-affix="clear">
            </div>
        </div>
        <div class="layui-btn-container ">
            <button class="layui-btn" lay-submit lay-filter="demo-table-search">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary">清空</button>
           @*  <button type="button" class="layui-btn layui-btn-primary layui-bg-blue" id="addData"> 添加</button> *@
        </div>
      
    </form>
    <div style="padding: 16px;">
    <table class="layui-hide" id="test"></table>
    </div>
    <div style="position: fixed; bottom: 0; left: 50%; transform: translateX(-50%);">
        <button class="layui-btn" onclick="add()">确定</button>
        <button class="layui-btn layui-btn-danger" onclick="cancel()">取消</button>
    </div>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="//unpkg.com/layui@2.9.17/dist/layui.js"></script>
    <script>

        var table = layui.table;
        var dropdown = layui.dropdown;
        var layer = layui.layer;
        var util = layui.util;
        function cancel() {
            var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引
            parent.layer.close(index);//再执行关闭
        }
        function add() {
            // 获取选中行数据并将名称传给父页面
            var checkStatus = table.checkStatus('test');
            var data = checkStatus.data;
            if (data.length == 0) {
                layer.msg("请选择数据");
                return;
            }
            if (data.length > 1) {
                layer.msg("只能选择一行数据");
                return;
            }

            var workorderCode = data[0].workorderCode;
            var workorderName = data[0].workorderName;
               console.log(workorderCode);
            // 通过父页面的接口将供应商名称传给父页面
            sessionStorage.setItem("workorderCodeFromPopup", workorderCode);
            sessionStorage.setItem("workorderNameFromPopup", workorderName);
            // 关闭当前弹出层
            cancel();
        }
        layui.use(function () {
            var table = layui.table;
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
             var laydate = layui.laydate;
            //显示
            function show() {
                table.render({
                    elem: '#test',
                    url: '/Song/GetProWorkorder',
                    //maxHeight: '530px',
                    //toolbar: '#TPL-treeTable-demo',
                     page: true, //开启分页
                     limit: 10,
                     limits: [10, 20, 50, 100],
                    cols: [[
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'workorderCode', title: '工单编码', width: 170, fixed: 'left' },
                        { field: 'workorderName', title: '工单名称', width: 120 },
                        {
                            field: 'workorderType', title: '工单类型', width: 150,templet: function (row) {
                                if (row.workorderType == 'SELF') {
                                    return "自产";
                                } else if (row.workorderType == 'OUTSOURCE') {
                                    return "外协";
                                } else  {
                                    return "外购";
                               }
                            }
                        },
                        {
                            field: 'orderSource', title: '工单来源', width: 150, templet: function (row) {
                                var tmp = row.orderSource == 'ORDER' ? "客户订单" : "库存备货";
                                return tmp;
                            }
                        },
                        {
                            field: 'requestDate', title: '需求日期', width: 150, templet: function (row) {
                                if (row.requestDate == null) {
                                    return "";
                                }
                                else {
                                    var tmp = row.requestDate.split("T")[0];
                                    return tmp;
                                }
                            }
                        },
                        {
                            field: 'status', title: '状态', width: 140, templet: function (row) {
                                var tmp = row.status == 'PREPARE' ? "草稿" : "已完成";
                                return tmp;
                            }
                        },
                        // {
                        //     fixed: "right", title: "操作", width: 180, align: "center", templet: function (row) {
                        //         if (row.status == 'PREPARE') {
                        //             var tmp = '<a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="detail">查看</a>';
                        //             tmp += '<a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delChild">删除</a>';
                        //         }else{
                        //             var tmp = '';
                        //         }
                        //         return tmp;
                        //     }
                        // }
                    ]],
                });
            }
            // 日期
            laydate.render({
                elem: '#recptDate'
            });
            show();//初始化显示
            // 搜索提交
            form.on('submit(demo-table-search)', function (data) {
                var WorkorderName = $("#WorkorderName").val();    //分类名称
                var WorkorderCode = $("#WorkorderCode").val();    //是否启用
                // 执行搜索重载
                table.reload('test', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: {
                        WorkorderName: WorkorderName,
                        WorkorderCode: WorkorderCode,
                    }// 搜索的字段
                });
                return false; // 阻止默认 form 跳转
            });

                // 添加分类
                    $("#addData").click(function () {
                      layer.open({
                        type: 2,
                    title: '添加生产工单',
                         shadeClose: true, // 点击遮罩区域，关闭弹层
                          scrollbar: false, // 暂时屏蔽浏览器滚动条
                        skin: 'layui-layer-rim', //加上边框
                        area: ['90%', '90%'], //宽高
                    content: '/Song/AddProductionOrder',
                        end: function () {
                            show();//刷新父级页面列表方法，类似于回调函数
                        }
                    });
                    });

            // 单元格工具事件
            table.on('tool(test)', function (obj) {
                var layEvent = obj.event; // 获得 lay-event 对应的值
                var trData = obj.data;
                if (layEvent === "detail") {
                    var data = obj.data;//获取表单中的点击按钮的那一行数据
                    console.log(data);
                    create_By = data.create_By;
                    creste_Time = data.creste_Time;
                    id = data.id;
                    orderSource = data.orderSource;
                    produceCode = data.produceCode;
                    produceName = data.produceName;
                    quantity = data.quantity;
                    remark = data.remark;
                    requestDate = data.requestDate;
                    status = data.status;
                    unifofMeasure = data.unifofMeasure;
                    workorderCode = data.workorderCode;
                    workorderName = data.workorderName;
                    workorderType = data.workorderType;
                    clientName = data.clientName;
                    layer.open({
                        title: '修改生产工单',
                        skin: 'layui-layer-rim', //加上边框
                        type: 2,
                         shadeClose: true, // 点击遮罩区域，关闭弹层
                          scrollbar: false, // 暂时屏蔽浏览器滚动条
                        area: ['80%', '80%'],
                        content: '/Song/UpdProductionOrder',
                        end: function () {
                            show();
                        }
                    });
                }  else if (layEvent === "delChild") {
                    layer.confirm("确定要删除吗？", function (data) {
                        var id = { id: trData.id };
                        // 此处可执行 Ajax 等操作
                        $.post('/Song/DeleteProWorkorder', id, function (res) {
                            if (res) {
                                layer.msg("删除成功", { icon: 1, time: 1000 }, function () {
                                    obj.del();
                                    // 刷新表格
                                    table.reload('test');
                                });
                            } else {
                                layer.msg("删除失败", { icon: 2, time: 1000 });
                            }
                        });
                    });
                }
            });
        });
    </script>
</body>
</html>